package com.smarth.zhizulian.jsinter;

import android.content.Context;
import android.webkit.JavascriptInterface;

import com.smarth.zhizulian.activity.LoginActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.bean.BaseEntity;
import com.smarth.zhizulian.bean.article.ArticleShare;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.ProgressObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.KumaLog;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.utils.ShareUtils;
import com.smarth.zhizulian.utils.UserUtils;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

/**
 * Created by 18865 on 2018/1/26.
 */

public class JSArticleInterface {
    private Context mContext;
    public JSArticleInterface(Context context) {
        this.mContext = context;
    }

    @JavascriptInterface
    public void shareTimeLine(long relationId) {
        KumaLog.kuma("relationId:" + relationId);
        //朋友圈
        if (UserUtils.isLogin()) {
            KumaHttp.getService().articleShare("" + relationId)
                    .compose(RxCompos.<ArticleShare>getBELoginCompos())
                    .subscribe(new ProgressObserver<ArticleShare>(mContext) {
                        @Override
                        public void onNext(ArticleShare articleShare) {
                            ShareUtils.shareToMomentWebPage(articleShare.getTitle(), articleShare.getDesc(), articleShare.getImage(), articleShare.getUrl(), new PlatformActionListener() {
                                @Override
                                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                    KumaToast.show(mContext, "分享成功");
                                    KumaHttp.getService().taskFinishTask(BaseConfig.Constant.TASK_SHAREARTICLECIRCLE)
                                            .compose(RxCompos.<Long>getBEAllLoginCompos())
                                            .subscribe(new ProgressObserver<BaseEntity<Long>>(mContext, false) {
                                                @Override
                                                public void onNext(BaseEntity<Long> o) {

                                                }
                                            });
                                }

                                @Override
                                public void onError(Platform platform, int i, Throwable throwable) {
                                    KumaToast.show(mContext, "分享失败");
                                }

                                @Override
                                public void onCancel(Platform platform, int i) {
                                    KumaToast.show(mContext, "取消分享");
                                }
                            });
                        }
                    });
        } else {
            LoginActivity.open(mContext);
        }
    }

    @JavascriptInterface
    public void shareFriend(long relationId) {
        KumaLog.kuma("relationId:" + relationId);
        //微信
        if (UserUtils.isLogin()) {
            KumaHttp.getService().articleShare("" + relationId)
                    .compose(RxCompos.<ArticleShare>getBELoginCompos())
                    .subscribe(new ProgressObserver<ArticleShare>(mContext) {
                        @Override
                        public void onNext(ArticleShare articleShare) {
                            ShareUtils.shareToWechatWebPage(articleShare.getTitle(), articleShare.getDesc(), articleShare.getImage(), articleShare.getUrl(), new PlatformActionListener() {
                                @Override
                                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                    KumaToast.show(mContext, "分享成功");
                                    KumaHttp.getService().taskFinishTask(BaseConfig.Constant.TASK_SHAREARTICLE)
                                            .compose(RxCompos.<Long>getBEAllLoginCompos())
                                            .subscribe(new ProgressObserver<BaseEntity<Long>>(mContext, false) {
                                                @Override
                                                public void onNext(BaseEntity<Long> o) {

                                                }
                                            });
                                }

                                @Override
                                public void onError(Platform platform, int i, Throwable throwable) {
                                    KumaToast.show(mContext, "分享失败");
                                }

                                @Override
                                public void onCancel(Platform platform, int i) {
                                    KumaToast.show(mContext, "取消分享");
                                }
                            });
                        }
                    });
        } else {
            LoginActivity.open(mContext);
        }
    }
}
